package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.Serviceitems;
import com.example.guiguihomemedical.entity.ServiceitemsDescription;
import com.example.guiguihomemedical.entity.Signing;
import com.example.guiguihomemedical.entity.dto.FulfillmentservicedetailData;
import com.example.guiguihomemedical.entity.dto.ServicePackServiceitems;
import com.example.guiguihomemedical.entity.dto.ServiceitemsDescriptionTotal;
import com.example.guiguihomemedical.mapper.FulfillmentMapper;
import com.example.guiguihomemedical.mapper.ServiceitemsMapper;
import com.example.guiguihomemedical.mapper.SigningMapper;
import com.example.guiguihomemedical.service.ServiceitemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ServiceitemsServiceImp implements ServiceitemsService {
    @Autowired
    private ServiceitemsMapper serviceitemsMapper;
    @Autowired
    private FulfillmentMapper fulfillmentMapper;
    @Autowired
    private SigningMapper signingMapper;

    @Override
    public List<Serviceitems> findAllServiceitems() {
        return serviceitemsMapper.findAllServiceitems();
    }

    @Override
    public Serviceitems findByIdServiceitems(int serviceItemsId) {
        return serviceitemsMapper.findByIdServiceitems(serviceItemsId);
    }

    @Override
    public List<ServiceitemsDescription> findAllServiceitemsDescription() {
        return serviceitemsMapper.findAllServiceitemsDescription();
    }

    @Override
    public List<ServicePackServiceitems> groupServiceitemsByServicePackId() {
        List<ServiceitemsDescription> allServiceitems = serviceitemsMapper.findAllServiceitemsDescription();

        // 使用 HashMap 来存储归类后的数据
        Map<String, List<ServiceitemsDescription>> groupedData = new HashMap<>();

        // 遍历所有服务项
        for (ServiceitemsDescription serviceitemsDescription : allServiceitems) {
            String servicePackName = serviceitemsDescription.getName();

            // 如果该 servicePackId 已存在于 groupedData 中，则将当前服务项添加到对应列表中
            if (groupedData.containsKey(servicePackName)) {
                groupedData.get(servicePackName).add(serviceitemsDescription);
            } else { // 如果该 servicePackId 不存在，则创建一个新的列表并将当前服务项添加到其中
                List<ServiceitemsDescription> itemList = new ArrayList<>();
                itemList.add(serviceitemsDescription);
                groupedData.put(servicePackName, itemList);
            }
        }

        List<ServicePackServiceitems> groupedServiceitems = new ArrayList<>();
        for (Map.Entry<String, List<ServiceitemsDescription>> entry : groupedData.entrySet()) {
            String servicePackName = entry.getKey();
            List<ServiceitemsDescription> items = entry.getValue();
            groupedServiceitems.add(new ServicePackServiceitems(servicePackName, items));
        }

        return groupedServiceitems;
    }

    @Override
    public List<FulfillmentservicedetailData> SbyIdgroupServiceitemsByServicePackId(String Idcard, int Filtervalues) {
        Signing signing = signingMapper.getByIdcardNowServing(Idcard);
        List<ServiceitemsDescription> allServiceitems = serviceitemsMapper.findByIdServiceitemsdetailed(Idcard, signing.getId());

        Map<String, List<ServiceitemsDescription>> groupedData = new HashMap<>();

        for (ServiceitemsDescription serviceitemsDescription : allServiceitems) {
            String servicePackName = serviceitemsDescription.getName();

            if (groupedData.containsKey(servicePackName)) {
                groupedData.get(servicePackName).add(serviceitemsDescription);
            } else {
                List<ServiceitemsDescription> itemList = new ArrayList<>();
                itemList.add(serviceitemsDescription);
                groupedData.put(servicePackName, itemList);
            }
        }

        List<ServicePackServiceitems> groupedServiceitems = new ArrayList<>();
        for (Map.Entry<String, List<ServiceitemsDescription>> entry : groupedData.entrySet()) {
            String servicePackName = entry.getKey();
            List<ServiceitemsDescription> items = entry.getValue();
            groupedServiceitems.add(new ServicePackServiceitems(servicePackName, items));
        }

        List<FulfillmentservicedetailData> fulfillmentservicedetailDataList = new ArrayList<>();
        for (ServicePackServiceitems serviceItem : groupedServiceitems) {
            FulfillmentservicedetailData data = new FulfillmentservicedetailData();
            List<ServiceitemsDescriptionTotal> serviceitemsList = new ArrayList<>();
            int allTotal = 0;
            int workTotal = 0;
            for (ServiceitemsDescription serviceitemsDescription : serviceItem.getServiceitemsList()) {
                Date nextTime;
                nextTime = fulfillmentMapper.findFulfillmentItemMaxNextTime(serviceitemsDescription.getServiceid(), Integer.parseInt(serviceitemsDescription.getServiceItemsId()), Idcard, signing.getId());
                int fulfillmentItemTotal = fulfillmentMapper.findFulfillmentItemTotal(serviceitemsDescription.getServiceid(), Integer.parseInt(serviceitemsDescription.getServiceItemsId()), Idcard, signing.getId());
                workTotal += fulfillmentItemTotal;
                allTotal += (fulfillmentItemTotal > 0 ? 1 : 0);

                ServiceitemsDescriptionTotal serviceitemsDescriptionTotal = new ServiceitemsDescriptionTotal();
                serviceitemsDescriptionTotal.setServiceid(serviceitemsDescription.getServiceid());
                serviceitemsDescriptionTotal.setIdcard(serviceitemsDescription.getIdcard());
                serviceitemsDescriptionTotal.setName(serviceitemsDescription.getName());
                serviceitemsDescriptionTotal.setServiceItemsName(serviceitemsDescription.getServiceItemsName());
                serviceitemsDescriptionTotal.setServiceItemsDescription(serviceitemsDescription.getServiceItemsDescription());
                serviceitemsDescriptionTotal.setServiceItemsId(serviceitemsDescription.getServiceItemsId());
                serviceitemsDescriptionTotal.setFulfillmentNexttime(nextTime);
                serviceitemsDescriptionTotal.setTotal(fulfillmentItemTotal);

                serviceitemsList.add(serviceitemsDescriptionTotal);
            }
            List<ServiceitemsDescriptionTotal> FilterList = new ArrayList<>();
            if (Filtervalues == 0) {
                for (ServiceitemsDescriptionTotal serviceitemsDescriptionTotal : serviceitemsList) {
                    if (serviceitemsDescriptionTotal.getTotal() == 0) {
                        data.setServicePackname(serviceItem.getServicePackname());
                        FilterList.add(serviceitemsDescriptionTotal);
                        data.setAllTotal(allTotal * 4);
                        data.setWorkTotal(workTotal);
                    }
                }
                data.setServiceitemsList(FilterList);
                if (data.getServicePackname() != null) {
                    fulfillmentservicedetailDataList.add(data);
                }
            } else if (Filtervalues == 1) {
                data.setServicePackname(serviceItem.getServicePackname());
                data.setServiceitemsList(serviceitemsList);
                data.setAllTotal(allTotal * 4);
                data.setWorkTotal(workTotal);
                fulfillmentservicedetailDataList.add(data);
            } else if (Filtervalues == 2) {
                for (ServiceitemsDescriptionTotal serviceitemsDescriptionTotal : serviceitemsList) {
                    if (serviceitemsDescriptionTotal.getTotal() != 0) {
                        data.setServicePackname(serviceItem.getServicePackname());
                        FilterList.add(serviceitemsDescriptionTotal);
                        data.setAllTotal(allTotal * 4);
                        data.setWorkTotal(workTotal);
                    }
                }
                data.setServiceitemsList(FilterList);
                if (data.getServicePackname() != null) {
                    fulfillmentservicedetailDataList.add(data);
                }
            }
        }

        return fulfillmentservicedetailDataList;
    }
}
